package com.hh.generator.core.generator;

import com.hh.generator.common.dto.ColumnEntity;
import com.hh.generator.common.dto.TableEntity;
import com.hh.generator.core.Generator;
import com.hh.generator.core.model.ClassModel;
import com.hh.generator.core.model.Config;
import com.hh.generator.core.model.FieldModel;

import java.util.List;
import java.util.stream.Collectors;

import static com.hh.generator.common.util.TableToClassConvertUtil.updateRequestClassName;

public class UpdateRequestGenerator extends AbstractTableBaseGenerator implements Generator {

    private String templateName = "UpdateRequest.java.vm";

    public UpdateRequestGenerator(Config config, TableEntity tableEntity, List<ColumnEntity> columnEntityList) {
        super(config, tableEntity, columnEntityList);
    }

    @Override
    public String fileName() {
        return updateRequestClassName(config.getTablePrefix(), tableEntity.getTableName()) + ".java";
    }


    @Override
    public ClassModel buildModel() {
        List<FieldModel> fieldModels = buildFieldModels();
        fieldModels = fieldModels.stream().filter(fieldModel -> !config.getEntityDefaultFields().contains(fieldModel.getFieldName())
                        || "id".equals(fieldModel.getFieldName()))
                .collect(Collectors.toList());
        List<String> imports = buildImports(fieldModels);
        ClassModel model = new ClassModel();
        model.setFields(fieldModels.stream().collect(Collectors.toList()));
        model.setPackageName(config.getRequestPackage().replace("xx", config.getModule()));
        model.setImports(imports);
        model.setComment(tableEntity.getTableComment() + " 更新请求对象");
        model.setAuthor(config.getClassAuthor());
        model.setDate(config.getDate());
        model.setClassName(updateRequestClassName(config.getTablePrefix(), tableEntity.getTableName()));
        model.setClassFullName(model.getPackageName() + "." + model.getClassName());
        return model;
    }

    @Override
    public String getTemplateName() {
        return templateName;
    }

}
